#include "TeacherMainwindow.h"
#include <qdialog.h>
#include "qstring.h"
#include "qtextedit.h"
#include "User.h"
#include "Takes.h"
#include "UserDB.h"
#include "TakesDB.h"
#include "Student.h"
#include "StudentDB.h"
#include "CourseDB.h"
#include "TeacherDB.h"
#include <QMessageBox>
#include <QTextCodec>  //添加头文
#include "InfoManageService.h"
#include "CourseManageService.h"
#include "StudentManagement.h"
#include "ScoreManagement.h"
#include <list>

TeacherMainwindow::TeacherMainwindow(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	showView(ui.userInfiView);
	InfoManageService infoService;
	string id = this->getLoginUserId().trimmed().toStdString();
	Teacher teach = infoService.lookTeacherInfo(id);
	ui.idEdit->setText(QString::fromStdString(teach.getId()));
	ui.nameEdit->setText(QString::fromStdString(teach.getName()));
	ui.deptNameEdit->setText(QString::fromStdString(teach.getDeptName()));
	
}

TeacherMainwindow::~TeacherMainwindow()
{
}

Ui::TeacherMainwindow TeacherMainwindow::getUi()
{
	return ui;
}

QString TeacherMainwindow::getLoginUserId()
{
	QString id = ui.loginUserLabel->text();
	return id;
}

void TeacherMainwindow::showView(QWidget *view)
{//显示一个页面
	
	ui.courseView->hide();//查看课程
	ui.scoreManageView->hide();//查看成绩
	ui.staticsticsView->hide();//成绩统计
	ui.studentCourseManageView->hide();//课程管理》学生课程
	ui.stuManageView->hide();//学生管理
	ui.updaPasswordView->hide();//修改密码
	ui.userInfiView->hide();//编辑用户
	

	/*
	ui.courseIdEdit->clear();
	ui.titleEdit->clear();
	*/
	ui.searchCourseEdit->clear();
	/*
	ui.scoreCourseIdEdit->clear();
	ui.scoreOrdinarlEdit->clear();
	ui.scoreStuIDEdit->clear();
	ui.socreFinalEdit->clear();
	*/
	ui.searchScoreEdit->clear();
	/*
	ui.scoreCourseIdEdit_2->clear();
	ui.scoreOrdinarlEdit_2->clear();
	ui.scoreStuIDEdit_2->clear();
	ui.socreFinalEdit_2->clear();
	*/
	ui.searchStuEdit->clear();
	/*
	ui.classEdit_2->clear();
	ui.deptNameEdit_2->clear();
	ui.sexEdit_2->clear();
	ui.stuIdEdit_2->clear();
	ui.stuNameEdit_2->clear();
	*/
	ui.searchStuCourseEdit->clear();
	/*
	ui.addStuCourseIDEdit->clear();
	ui.addStuIDEdit->clear();
	*/

	/*ui.newpwEdit->clear();
	ui.newpwrEdit->clear();
	ui.oldpwEdit->clear();
	ui.deptNameEdit->clear();
	ui.idEdit->clear();
	ui.nameEdit->clear();

	*/
	view->show();
}

void TeacherMainwindow::on_teacherMenuTree_itemClicked(QTreeWidgetItem *item, int column)
{//教师菜单
	QString s = item->text(column);

	if (s == "修改密码") {
		showView(ui.updaPasswordView);
	}
	else if (s == "个人信息") {//信息管理
		showView(ui.userInfiView);

		InfoManageService infoService;
		string id = this->getLoginUserId().trimmed().toStdString();
		Teacher teach = infoService.lookTeacherInfo(id);
		ui.idEdit->setText(QString::fromStdString(teach.getId()));
		ui.nameEdit->setText(QString::fromStdString(teach.getName()));
		ui.deptNameEdit->setText(QString::fromStdString(teach.getDeptName()));
	}
	else if (s == "查看学生" || s == "增删学生") {//学生管理
		updateStuTable();
	}
	else if (s == "查看成绩" || s == "处理成绩") {//成绩管理
		updatestudentScoreTable();
	}
	else if (s == "成绩统计") {//成绩管理>成绩统计
		updateStaticstics();
	}
	else if (s == "查看课程" || s.contains("教师开")) {//课程管理
		updateCourseTable();
	}
	else if (s == "课程学生") {//课程管理>课程学生
		updateStuCourseTable();
	}
	else {
		//
	}
}

//信息管理
void TeacherMainwindow::on_changepwBtn_clicked()
{//修改密码
	string id = this->getLoginUserId().trimmed().toStdString();

	string oldpw = ui.oldpwEdit->text().trimmed().toStdString();
	string newpw = ui.newpwEdit->text().trimmed().toStdString();
	string newrpw = ui.newpwrEdit->text().trimmed().toStdString();
	UserDB uDB;
	User u = uDB.selectByUserId(id);
	if (oldpw == "" || newpw == "" || newrpw == "") {
		QMessageBox::warning(this, tr("Waring"), tr("输入不能为空!"), QMessageBox::Yes);
	}
	else if (newpw != newrpw)
	{
		QMessageBox::warning(this, tr("Waring"), tr("两次输入的密码不正确!"), QMessageBox::Yes);
	}
	else if (u.getPassword() != oldpw) {
		QMessageBox::warning(this, tr("Waring"), tr("旧密码不正确!"), QMessageBox::Yes);
	}
	else if (u.getPassword() == oldpw && newpw == newrpw) {
		u.setPassword(newpw);
		uDB.update(u);
		QMessageBox::warning(this, tr("Waring"), tr("修改密码成功!"), QMessageBox::Yes);
		ui.newpwEdit->clear();
		ui.newpwrEdit->clear();
		ui.oldpwEdit->clear();
	}

}
void TeacherMainwindow::on_saveTeacherBtn_clicked()
{//编辑个人信息
	string id = this->getLoginUserId().trimmed().toStdString();


	string name = ui.nameEdit->text().trimmed().toStdString();
	string deptName = ui.deptNameEdit->text().trimmed().toStdString();

	InfoManageService info;
	Teacher teacher;


	if (id == "" || name == "" || deptName == "") {
		QMessageBox::warning(this, tr("Waring"), tr("输入不能为空!"), QMessageBox::Yes);
	}
	else {
		teacher.setId(id);
		teacher.setName(name);
		teacher.setDeptName(deptName);
		info.teacEditInfo(teacher);
		QMessageBox::warning(this, tr("Waring"), tr("个人信息修改成功!"), QMessageBox::Yes);
	}
}

//学生管理
void TeacherMainwindow::updateStuTable()
{//更新学生表格
	showView(ui.stuManageView);
	ui.addStuWidget->hide();

	StudentManagement stuManage;
	list<Student> list = stuManage.CheckAllStudentMess();
	string id = this->getLoginUserId().trimmed().toStdString();
	QTableWidget *table = ui.stuTable;
	table->setRowCount(list.size());
	table->setColumnWidth(0, 300);//学号
	table->setColumnWidth(1, 200);//姓名
	table->setColumnWidth(2, 200);//性别
	table->setColumnWidth(3, 200);//班级
	table->setColumnWidth(4, 250);//学院
	table->setColumnWidth(5, 150);//操作
	int i = 0;
	for each(Student s in list) {
		table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(s.getId())));//第一行第一列的内容设置
		table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(s.getName())));
		table->setItem(i, 2, new QTableWidgetItem(QString::fromStdString(s.getSex())));
		table->setItem(i, 3, new QTableWidgetItem(QString::fromStdString(s.getClassName())));
		table->setItem(i, 4, new QTableWidgetItem(QString::fromStdString(s.getDeptName())));

		QTableWidgetItem *check = new QTableWidgetItem();
		check->setCheckState(Qt::Unchecked);
		table->setItem(i, 5, check);
		i++;
	}
}
void TeacherMainwindow::on_stuTable_cellChanged(int row, int col)
{//学生管理

}
void TeacherMainwindow::on_addStuBtn_clicked()
{//添加学生
	ui.addStuWidget->show();
}
void TeacherMainwindow::on_saveStuBtn_clicked()
{//添加保存
	string sid = ui.stuIdEdit_2->text().trimmed().toStdString();
	string name = ui.stuNameEdit_2->text().trimmed().toStdString();
	string sex = ui.sexEdit_2->text().trimmed().toStdString();
	string className = ui.classEdit_2->text().trimmed().toStdString();
	string deptName = ui.deptNameEdit_2->text().trimmed().toStdString();
	string password = "123";
	StudentManagement stuService;
	string result = stuService.AddStudent(sid, name, sex, className, deptName, password);
	QMessageBox::warning(this, tr("提示"), QString::fromStdString(result), QMessageBox::Yes);
	updateStuTable();
}
void TeacherMainwindow::on_deleteStuBtn_clicked()
{//删除学生
	StudentManagement stuService;
	QMessageBox message(QMessageBox::NoIcon, "确认", "你确定删除该学生?", QMessageBox::Yes | QMessageBox::No, NULL);
	if (message.exec() == QMessageBox::Yes)
	{
		int count = 0;
		string id;
		int row = ui.stuTable->rowCount();
		for (int i = 0; i < row; i++) {
			if (ui.stuTable->item(i, 5)->checkState() == Qt::Checked) {
				count++;
				if (count == 1) {
					id = ui.stuTable->item(i, 0)->text().toStdString();
				}
			}
		}
		if (count == 1) {
			string result = stuService.DeleteStudent(id);
			QMessageBox::warning(this, tr("提示"), QString::fromStdString(result), QMessageBox::Yes);
			updateStuTable();
		}
		else {
			QMessageBox::warning(this, tr("提示"), tr("只能选择一个学生进行删除！"), QMessageBox::Yes);
		}
	}
}
void TeacherMainwindow::on_unSaveStuBtn_clicked()
{//取消保存
	ui.addStuWidget->hide();
	ui.stuIdEdit_2->clear();
	ui.stuNameEdit_2->clear();
	ui.sexEdit_2->clear();
	ui.classEdit_2->clear();
	ui.deptNameEdit_2->clear();
}
void TeacherMainwindow::on_searchStuBtn_clicked()
{//搜索学生
	string key = ui.searchStuEdit->text().toStdString();
	if (key != "") {
		showView(ui.stuManageView);
		ui.addStuWidget->hide();

		StudentDB sDB;
		list<Student> list = sDB.selectByLike(key);
		string id = this->getLoginUserId().trimmed().toStdString();
		QTableWidget *table = ui.stuTable;
		table->setRowCount(list.size());
		table->setColumnWidth(0, 300);//学号
		table->setColumnWidth(1, 200);//姓名
		table->setColumnWidth(2, 200);//性别
		table->setColumnWidth(3, 200);//班级
		table->setColumnWidth(4, 250);//学院
		table->setColumnWidth(5, 150);//操作
		int i = 0;
		for each(Student s in list) {
			table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(s.getId())));//第一行第一列的内容设置
			table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(s.getName())));
			table->setItem(i, 2, new QTableWidgetItem(QString::fromStdString(s.getSex())));
			table->setItem(i, 3, new QTableWidgetItem(QString::fromStdString(s.getClassName())));
			table->setItem(i, 4, new QTableWidgetItem(QString::fromStdString(s.getDeptName())));

			QTableWidgetItem *check = new QTableWidgetItem();
			check->setCheckState(Qt::Unchecked);
			table->setItem(i, 5, check);
			i++;
		}
	}
	else {
		QMessageBox::warning(this, tr("提示"), tr("搜索内容不能为空"), QMessageBox::Yes);
	}
}

//课程管理
void TeacherMainwindow::updateCourseTable()
{//更新courseTable
	showView(ui.courseView);
	ui.addCourseWidget->hide();

	string tid = this->getLoginUserId().trimmed().toStdString();
	CourseManageService courseService;
	list<Course> list = courseService.lookMyCourses(tid);
	QTableWidget *table = ui.courseTable;
	table->setRowCount(list.size());
	table->setColumnWidth(0, 400);//课程ID
	table->setColumnWidth(1, 600);//课程名
	table->setColumnWidth(2, 200);//选择
	int i = 0;
	for each(Course c in list) {
		table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(c.getCourseId())));//第一行第一列的内容设置
		table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(c.getTitle())));

		QTableWidgetItem *check = new QTableWidgetItem();
		check->setCheckState(Qt::Unchecked);
		table->setItem(i, 2, check);
		i++;
	}
}
void TeacherMainwindow::on_addCourseBtn_clicked()
{//添加课程
	ui.addCourseWidget->show();
}
void TeacherMainwindow::on_saveCourseBtn_clicked()
{//保存课程
	string courseId = ui.courseIdEdit->text().trimmed().toStdString();
	string title = ui.titleEdit->text().trimmed().toStdString();
	string tid = this->getLoginUserId().trimmed().toStdString();

	CourseManageService courseService;
	string result = courseService.TeacherOpenCourse(courseId, title, tid);
	QMessageBox::warning(this, tr("提示"), QString::fromStdString(result), QMessageBox::Yes);
	updateCourseTable();
}
void TeacherMainwindow::on_deleteCourseBtn_clicked()
{//删除课程
	string tid = this->getLoginUserId().trimmed().toStdString();
	CourseManageService courseService;
	QMessageBox message(QMessageBox::NoIcon, "确认", "你确定删除该课程?", QMessageBox::Yes | QMessageBox::No, NULL);
	if (message.exec() == QMessageBox::Yes)
	{
		int count = 0;
		string cid;
		int row = ui.courseTable->rowCount();
		for (int i = 0; i < row; i++) {
			if (ui.courseTable->item(i, 2)->checkState() == Qt::Checked) {
				count++;
				if (count == 1) {
					cid = ui.courseTable->item(i, 0)->text().toStdString();
				}
			}
		}
		if (count == 1) {
			string result = courseService.TeacherDeleteCourse(tid, cid);
			QMessageBox::warning(this, tr("提示"), QString::fromStdString(result), QMessageBox::Yes);
			updateCourseTable();
		}
		else {
			QMessageBox::warning(this, tr("提示"), tr("只能选择一个课程进行删除！"), QMessageBox::Yes);
		}
	}
}
void TeacherMainwindow::on_unSaveCourseBtn_clicked()
{//取消保存
	ui.courseIdEdit->clear();
	ui.titleEdit->clear();
	ui.addCourseWidget->hide();
}
void TeacherMainwindow::on_searchCourseBtn_clicked()
{//搜索课程
	string key = ui.searchCourseEdit->text().toStdString();
	if (key != "") {
		showView(ui.courseView);
		ui.addCourseWidget->hide();

		CourseDB cDB;
		list<Course> list = cDB.selectByLike2(key);

		QTableWidget *table = ui.courseTable;
		table->setRowCount(list.size());
		table->setColumnWidth(0, 400);//课程ID
		table->setColumnWidth(1, 600);//课程名
		table->setColumnWidth(2, 200);//选择
		int i = 0;
		for each(Course c in list) {
			table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(c.getCourseId())));//第一行第一列的内容设置
			table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(c.getTitle())));

			QTableWidgetItem *check = new QTableWidgetItem();
			check->setCheckState(Qt::Unchecked);
			table->setItem(i, 2, check);
			i++;
		}
	}
	else {
		QMessageBox::warning(this, tr("提示"), tr("搜索内容不能为空"), QMessageBox::Yes);
	}
}
void TeacherMainwindow::on_courseTable_cellChanged(int row, int col)
{//课程表格改变

}

//课程学生管理  
void TeacherMainwindow::updateStuCourseTable()
{
	showView(ui.studentCourseManageView);
	ui.addCourseStuWidget->hide();
	string tid = this->getLoginUserId().trimmed().toStdString();
	CourseManageService courseService;
	list<Course> clist = courseService.lookMyCourses(tid);
	QTableWidget *table = ui.stuCourseTable;
	table->setColumnWidth(0, 200);//课程ID
	table->setColumnWidth(1, 400);//课程名
	table->setColumnWidth(2, 200);//学号
	table->setColumnWidth(3, 150);//姓名
	table->setColumnWidth(4, 200);//班级
	table->setColumnWidth(5, 100);//选择
	int i = 0;
	for each(Course c in clist) {
		list<Student> slist = courseService.lookStuInCourse(c.getCourseId());
		for each(Student s in slist)
		{
			table->setRowCount(i + 1);
			table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(c.getCourseId())));//第一行第一列的内容设置
			table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(c.getTitle())));
			table->setItem(i, 2, new QTableWidgetItem(QString::fromStdString(s.getId())));
			table->setItem(i, 3, new QTableWidgetItem(QString::fromStdString(s.getName())));
			table->setItem(i, 4, new QTableWidgetItem(QString::fromStdString(s.getClassName())));

			QTableWidgetItem *check = new QTableWidgetItem();
			check->setCheckState(Qt::Unchecked);
			table->setItem(i, 5, check);
			i++;
		}
	}
}
void TeacherMainwindow::on_addStuCourseBtn_clicked()
{//添加课程学生
	ui.addCourseStuWidget->show();
}
void TeacherMainwindow::on_saveAddStuCourseBtn_clicked()
{//保存课程学生
	string cid = ui.addStuCourseIDEdit->text().toStdString();
	string sid = ui.addStuIDEdit->text().toStdString();
	CourseManageService courseService;
	string result = courseService.TeacherAddStuIntoCourse(cid, sid);
	QMessageBox::warning(this, tr("提示"), QString::fromStdString(result), QMessageBox::Yes);
	updateStuCourseTable();
}
void TeacherMainwindow::on_deleteStuCourseBtn_clicked()
{//删除课程学生
	string tid = this->getLoginUserId().trimmed().toStdString();
	CourseManageService courseService;
	QMessageBox message(QMessageBox::NoIcon, "确认", "你确定删除该课程的该同学?", QMessageBox::Yes | QMessageBox::No, NULL);
	if (message.exec() == QMessageBox::Yes)
	{
		int count = 0;
		string cid;
		string sid;
		int row = ui.stuCourseTable->rowCount();
		for (int i = 0; i < row; i++) {
			if (ui.stuCourseTable->item(i, 5)->checkState() == Qt::Checked) {
				count++;
				if (count == 1) {
					cid = ui.stuCourseTable->item(i, 0)->text().toStdString();
					sid = ui.stuCourseTable->item(i, 2)->text().toStdString();
				}
			}
		}
		if (count == 1) {
			string result = courseService.TeacherDeleteStuFromCourse(sid, cid);
			QMessageBox::warning(this, tr("提示"), QString::fromStdString(result), QMessageBox::Yes);
			updateStuCourseTable();
		}
		else {
			QMessageBox::warning(this, tr("提示"), tr("只能选择一个学生进行删除！"), QMessageBox::Yes);
		}
	}
}
void TeacherMainwindow::on_unSaveAddStuCourseBtn_clicked()
{//取消保存
	ui.addCourseStuWidget->hide();
	ui.addStuCourseIDEdit->clear();
	ui.addStuIDEdit->clear();
}
void TeacherMainwindow::on_searchStuCourseBtn_clicked()
{//搜索课程学生
	string key = ui.searchStuCourseEdit->text().toStdString();
	if (key != "") {
		showView(ui.studentCourseManageView);
		ui.addCourseStuWidget->hide();

		string tid = this->getLoginUserId().trimmed().toStdString();
		CourseManageService courseService;
		CourseDB cDB;
		list<Course> clist = cDB.selectByLike2(key);
		QTableWidget *table = ui.stuCourseTable;
		table->setColumnWidth(0, 200);//课程ID
		table->setColumnWidth(1, 400);//课程名
		table->setColumnWidth(2, 200);//学号
		table->setColumnWidth(3, 150);//姓名
		table->setColumnWidth(4, 200);//班级
		table->setColumnWidth(5, 100);//选择
		int i = 0;
		for each(Course c in clist) {
			list<Student> slist = courseService.lookStuInCourse(c.getCourseId());
			for each(Student s in slist)
			{
				table->setRowCount(i + 1);
				table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(c.getCourseId())));//第一行第一列的内容设置
				table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(c.getTitle())));
				table->setItem(i, 2, new QTableWidgetItem(QString::fromStdString(s.getId())));
				table->setItem(i, 3, new QTableWidgetItem(QString::fromStdString(s.getName())));
				table->setItem(i, 4, new QTableWidgetItem(QString::fromStdString(s.getClassName())));

				QTableWidgetItem *check = new QTableWidgetItem();
				check->setCheckState(Qt::Unchecked);
				table->setItem(i, 5, check);
				i++;
			}
		}
	}
	else {
		QMessageBox::warning(this, tr("提示"), tr("搜索内容不能为空"), QMessageBox::Yes);
	}
}
void TeacherMainwindow::on_stuCourseTable_cellChanged(int row, int col)
{

}

//成绩管理
void TeacherMainwindow::updatestudentScoreTable()
{//更新成绩表
	showView(ui.scoreManageView);
	ui.addScoreWidget->hide();
	ui.editScoreWidget->hide();
	QTableWidget *table = ui.studentScoreTable;
	table->setColumnWidth(0, 100);//课程ID
	table->setColumnWidth(1, 300);//课程名
	table->setColumnWidth(2, 200);//学号
	table->setColumnWidth(3, 100);//姓名
	table->setColumnWidth(4, 150);//班级
	table->setColumnWidth(5, 100);//平时成绩
	table->setColumnWidth(6, 100);//期末成绩
	table->setColumnWidth(7, 100);//总评成绩
	table->setColumnWidth(8, 100);//选择

	string tid = this->getLoginUserId().trimmed().toStdString();
	CourseManageService courseService;
	list<Course> clist;
	clist.clear();
	clist = courseService.lookMyCourses(tid);
	ScoreManagement scoreService;
	table->setRowCount(0);//刷新表格数据
	int i = 0;
	for each(Course c in clist) {
		list<Student> slist;
		slist.clear();
		slist = courseService.lookStuInCourse(c.getCourseId());
		for each(Student s in slist)
		{
			table->setRowCount(i + 1);
			table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(c.getCourseId())));//第一行第一列的内容设置
			table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(c.getTitle())));
			table->setItem(i, 2, new QTableWidgetItem(QString::fromStdString(s.getId())));
			table->setItem(i, 3, new QTableWidgetItem(QString::fromStdString(s.getName())));
			table->setItem(i, 4, new QTableWidgetItem(QString::fromStdString(s.getClassName())));

			Grade g = scoreService.TeaCheckOneScore(s.getId(), c.getCourseId());
			if (g.getGradeId() != "")
			{
				table->setItem(i, 5, new QTableWidgetItem(QString::number(g.getOrdinary(), 10, 2)));
				table->setItem(i, 6, new QTableWidgetItem(QString::number(g.getGfinal(), 10, 2)));
				table->setItem(i, 7, new QTableWidgetItem(QString::number(g.getTotal(), 10, 2)));
			}

			QTableWidgetItem *check = new QTableWidgetItem();
			check->setCheckState(Qt::Unchecked);
			table->setItem(i, 8, check);
			i++;
		}
	}
}
void TeacherMainwindow::on_editScoreBtn_clicked()
{//修改成绩
	int count = 0;
	QString cid;
	QString sid;
	QString ordinary ="";
	QString final="";
	int row = ui.studentScoreTable->rowCount();
	for (int i = 0; i < row; i++) {
		if (ui.studentScoreTable->item(i, 8)->checkState() == Qt::Checked) {
			count++;
			if (count == 1) {
				cid = ui.studentScoreTable->item(i, 0)->text();
				sid = ui.studentScoreTable->item(i, 2)->text();
				if (ui.studentScoreTable->item(i, 5) != NULL && ui.studentScoreTable->item(i, 6) != NULL) {
					ordinary = ui.studentScoreTable->item(i, 5)->text();
					final = ui.studentScoreTable->item(i, 6)->text();
				}
			}
		}
	}
	if (count == 1) {
		if (ordinary == "" || final == "") {
			QMessageBox::warning(this, tr("提示"), tr("该学生无成绩无法修改!"), QMessageBox::Yes);
		}
		else {
			ui.scoreCourseIdEdit_2->setText(cid);
			ui.scoreStuIDEdit_2->setText(sid);
			ui.scoreOrdinarlEdit_2->setText(ordinary);
			ui.socreFinalEdit_2->setText(final);
			ui.editScoreWidget->show();
		}
	}
	else {
		QMessageBox::warning(this, tr("提示"), tr("只能选择一个学生进行成绩修改！"), QMessageBox::Yes);
	}
}
void TeacherMainwindow::on_addScoreBtn_clicked()
{//添加成绩
	int count = 0;
	QString cid;
	QString sid;
	int row = ui.studentScoreTable->rowCount();
	for (int i = 0; i < row; i++) {
		if (ui.studentScoreTable->item(i, 8)->checkState() == Qt::Checked) {
			count++;
			if (count == 1) {
				cid = ui.studentScoreTable->item(i, 0)->text();
				sid = ui.studentScoreTable->item(i, 2)->text();
			}
		}
	}
	if (count == 1) {
		ui.scoreCourseIdEdit->setText(cid);
		ui.scoreStuIDEdit->setText(sid);
		ui.addScoreWidget->show();
	}
	else {
		QMessageBox::warning(this, tr("提示"), tr("只能选择一个学生进行添加成绩！"), QMessageBox::Yes);
	}
}
void TeacherMainwindow::on_saveScoreBtn_clicked()
{//保存成绩
	ScoreManagement scoreManagement;
	string cid = ui.scoreCourseIdEdit->text().toStdString();
	string sid = ui.scoreStuIDEdit->text().toStdString();
	double ordinary = ui.scoreOrdinarlEdit->text().toDouble();
	double tFinal = ui.socreFinalEdit->text().toDouble();
	string result = scoreManagement.TeaAddStuScore(sid, cid, ordinary, tFinal);
	QMessageBox::warning(this, tr("提示"), QString::fromStdString(result), QMessageBox::Yes);
	updatestudentScoreTable();
}
void TeacherMainwindow::on_saveScoreBtn_2_clicked()
{//保存修改成绩
	ScoreManagement scoreManagement;
	string cid = ui.scoreCourseIdEdit_2->text().toStdString();
	string sid = ui.scoreStuIDEdit_2->text().toStdString();
	double ordinary = ui.scoreOrdinarlEdit_2->text().toDouble();
	double tFinal = ui.socreFinalEdit_2->text().toDouble();
	string result = scoreManagement.TeaModifyStuScore(sid, cid, ordinary, tFinal);
	QMessageBox::warning(this, tr("提示"), QString::fromStdString(result), QMessageBox::Yes);
	updatestudentScoreTable();
}
void TeacherMainwindow::on_deleteScoreBtn_clicked()
{//删除学生成绩
	ScoreManagement scoreManagement;
	QMessageBox message(QMessageBox::NoIcon, "确认", "你确定删除该课程该同学的成绩?", QMessageBox::Yes | QMessageBox::No, NULL);
	if (message.exec() == QMessageBox::Yes)
	{
		int count = 0;
		string cid;
		string sid;
		int row = ui.studentScoreTable->rowCount();
		for (int i = 0; i < row; i++) {
			if (ui.studentScoreTable->item(i, 8)->checkState() == Qt::Checked) {
				count++;
				if (count == 1) {
					cid = ui.studentScoreTable->item(i, 0)->text().toStdString();
					sid = ui.studentScoreTable->item(i, 2)->text().toStdString();
				}
			}
		}
		if (count == 1) {
			string result = scoreManagement.TeaDeleteStuScore(sid, cid);
			QMessageBox::warning(this, tr("提示"), QString::fromStdString(result), QMessageBox::Yes);
			updatestudentScoreTable();
		}
		else {
			QMessageBox::warning(this, tr("提示"), tr("只能选择一个学生进行删除！"), QMessageBox::Yes);
		}
	}
}
void TeacherMainwindow::on_unSaveScoreBtn_clicked()
{//取消保存
	ui.addScoreWidget->hide();
}
void TeacherMainwindow::on_unSaveScoreBtn_2_clicked()
{//取消保存
	ui.editScoreWidget->hide();
}
void TeacherMainwindow::on_searchScoreBtn_clicked()
{//搜索学生成绩

	string key = ui.searchScoreEdit->text().toStdString();
	if (key != "") {

		showView(ui.scoreManageView);
		ui.addScoreWidget->hide();
		QTableWidget *table = ui.studentScoreTable;
		table->setColumnWidth(0, 100);//课程ID
		table->setColumnWidth(1, 300);//课程名
		table->setColumnWidth(2, 200);//学号
		table->setColumnWidth(3, 100);//姓名
		table->setColumnWidth(4, 150);//班级
		table->setColumnWidth(5, 100);//平时成绩
		table->setColumnWidth(6, 100);//期末成绩
		table->setColumnWidth(7, 100);//总评成绩
		table->setColumnWidth(8, 100);//选择

									  //string tid = this->getLoginUserId().trimmed().toStdString();
		CourseManageService courseService;
		CourseDB cDB;
		list<Course> clist = cDB.selectByLike2(key);
		ScoreManagement scoreService;

		int i = 0;
		for each(Course c in clist) {
			list<Student> slist = courseService.lookStuInCourse(c.getCourseId());
			for each(Student s in slist)
			{
				table->setRowCount(i + 1);
				table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(c.getCourseId())));//第一行第一列的内容设置
				table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(c.getTitle())));
				table->setItem(i, 2, new QTableWidgetItem(QString::fromStdString(s.getId())));
				table->setItem(i, 3, new QTableWidgetItem(QString::fromStdString(s.getName())));
				table->setItem(i, 4, new QTableWidgetItem(QString::fromStdString(s.getClassName())));

				Grade g = scoreService.TeaCheckOneScore(s.getId(), c.getCourseId());
				if (g.getGradeId() != "")
				{
					table->setItem(i, 5, new QTableWidgetItem(QString::number(g.getOrdinary(), 10, 2)));
					table->setItem(i, 6, new QTableWidgetItem(QString::number(g.getGfinal(), 10, 2)));
					table->setItem(i, 7, new QTableWidgetItem(QString::number(g.getTotal(), 10, 2)));
				}

				QTableWidgetItem *check = new QTableWidgetItem();
				check->setCheckState(Qt::Unchecked);
				table->setItem(i, 8, check);
				i++;
			}
		}

	}
	else {
		QMessageBox::warning(this, tr("提示"), tr("搜索内容不能为空"), QMessageBox::Yes);
	}
}
void TeacherMainwindow::on_studentScoreTable_cellChanged(int row, int col)
{//学生成绩表改变

}

//成绩统计
void TeacherMainwindow::updateStaticstics()
{//刷新统计表格
	showView(ui.staticsticsView);

	string tid = this->getLoginUserId().trimmed().toStdString();
	QTableWidget *table = ui.tableWidget;

	table->setColumnWidth(0, 170);//课程ID
	table->setColumnWidth(1, 150);//课程名
	table->setColumnWidth(2, 150);//最高分
	table->setColumnWidth(3, 150);//最低分
	table->setColumnWidth(4, 150);//平均分
	table->setColumnWidth(5, 150);//优秀率
	table->setColumnWidth(6, 150);//良好率
	table->setColumnWidth(7, 220);//不及格率

	CourseManageService courseMS;//获取老师所有的课程
	list<Course> listCourse = courseMS.lookMyCourses(tid);
	table->setRowCount(listCourse.size());

	int i = 0;//用于标志当前行
	for each(Course tc in listCourse) {//教师开课的所有课程的统计
		string cid = tc.getCourseId();
		string cName = tc.getTitle();

		//QMessageBox::warning(this, tr("提示"), tr("搜索内容不能为空"), QMessageBox::Yes);

		ScoreManagement scoreManage;
		list<double> listS;
		listS = scoreManage.TeaAnalysisScore(cid);


		table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(cid)));//第一行第一列的内容设置
		table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(cName)));

		if (listS.size()==6) {
			int j = 0;
			for each(double d in listS)
			{
				//QMessageBox::warning(this, tr("提示"), tr("搜索内容不能为空"), QMessageBox::Yes);
				if (j == 0) {
					table->setItem(i, 2, new QTableWidgetItem(QString::number(d, 10, 2)));
				}
				else if (j == 1) {
					table->setItem(i, 3, new QTableWidgetItem(QString::number(d, 10, 2)));
				}
				else if (j == 2) {
					table->setItem(i, 4, new QTableWidgetItem(QString::number(d, 10, 2)));
				}
				else if (j == 3) {
					table->setItem(i, 5, new QTableWidgetItem(QString::number(d, 10, 2)));
				}
				else if (j == 4) {
					table->setItem(i, 6, new QTableWidgetItem(QString::number(d, 10, 2)));
				}
				else if (j == 5) {
					table->setItem(i, 7, new QTableWidgetItem(QString::number(d, 10, 2)));
				}
				j++;
			}
		}
		i++;
	}
}


